<template>
  <div class="p-[var(--app-content-padding)]">
    <ContentWrap class="form-card">
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px mt-[4px]"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
      >
        <el-form-item label="是否已读" prop="readStatus">
          <el-select
            v-model="queryParams.readStatus"
            placeholder="请选择状态"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发送时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item class="float-right !mr-0">
          <el-button @click="handleQuery" type="primary">
            <Icon icon="ep:search" /> 
            <span>搜索</span>
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" /> 
            <span>重置</span>
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap class="mb-0">
      <div class="flex justify-between mb-[12px]">
        <div>
          <el-button @click="handleUpdateList" :disabled="!selectedIds.length" type="primary">
            <Icon icon="ep:reading" />
            <span class="mt-[1px]">标记已读</span>
          </el-button>
          <el-button @click="handleUpdateAll" type="primary" plain>
            <Icon icon="ep:reading" /> 
            <span class="mt-[1px]">全部已读</span>
          </el-button>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="list"
        ref="tableRef"
        row-key="id"
        @selection-change="handleSelectionChange" 
        :height="tableHeight"
        show-overflow-tooltip
      >
        <el-table-column type="selection" :selectable="selectable" :reserve-selection="true" />
        <el-table-column label="发送人" align="center" prop="templateNickname" />
        <el-table-column
          label="发送时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
        />
        <el-table-column label="类型" align="center" prop="templateType">
          <template #default="scope">
            <dict-tag
              :type="DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE"
              :value="scope.row.templateType"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="消息内容"
          align="center"
          prop="templateContent"
          show-overflow-tooltip
        />
        <el-table-column label="是否已读" align="center" prop="readStatus">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.readStatus" />
          </template>
        </el-table-column>
        <el-table-column
          label="阅读时间"
          align="center"
          prop="readTime"
          :formatter="dateFormatter"
        />
        <el-table-column label="操作" align="center">
          <template #default="scope">

            <el-tooltip v-if="scope.row.readStatus" content="详情" placement="top">
              <el-button
                link
                type="primary"
                @click="openDetail(scope.row)"
              >
                <Icon icon="bx:message-detail" />
              </el-button>
            </el-tooltip>
            <el-tooltip v-else content="已读" placement="top">
              <el-button
                link
                type="warning"
                @click="openDetail(scope.row)"
              >
                <Icon icon="fluent:mail-read-24-regular" />
              </el-button>
            </el-tooltip>

          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </div>

  <!-- 表单弹窗：详情 -->
  <MyNotifyMessageDetail ref="detailRef" />
</template>

<script lang="ts" setup>
import { DICT_TYPE, getBoolDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as NotifyMessageApi from '@/api/system/notify/message'
import MyNotifyMessageDetail from './MyNotifyMessageDetail.vue'
import { useTableHeight } from '@/hooks/web/useTableHeight'

defineOptions({ name: 'SystemMyNotify' })

const message = useMessage() // 消息

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  readStatus: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

const { tableHeight } = useTableHeight({
  extraHeight: 208
})

const tableRef = ref() // 表格的 Ref
const selectedIds = ref<number[]>([]) // 表格的选中 ID 数组

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await NotifyMessageApi.getMyNotifyMessagePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  tableRef.value.clearSelection()
  handleQuery()
}

/** 详情操作 */
const detailRef = ref()
const openDetail = (data: NotifyMessageApi.NotifyMessageVO) => {
  if (!data.readStatus) {
    handleReadOne(data.id)
  }
  detailRef.value.open(data)
}

/** 标记一条站内信已读 */
const handleReadOne = async (id) => {
  await NotifyMessageApi.updateNotifyMessageRead(id)
  await getList()
}

/** 标记全部站内信已读 **/
const handleUpdateAll = async () => {
  await NotifyMessageApi.updateAllNotifyMessageRead()
  message.success('全部已读成功！')
  tableRef.value.clearSelection()
  await getList()
}

/** 标记一些站内信已读 **/
const handleUpdateList = async () => {
  if (selectedIds.value.length === 0) {
    return
  }
  await NotifyMessageApi.updateNotifyMessageRead(selectedIds.value)
  message.success('批量已读成功！')
  tableRef.value.clearSelection()
  await getList()
}

/** 某一行，是否允许选中 */
const selectable = (row) => {
  return !row.readStatus
}

/** 当表格选择项发生变化时会触发该事件  */
const handleSelectionChange = (array: NotifyMessageApi.NotifyMessageVO[]) => {
  selectedIds.value = []
  if (!array) {
    return
  }
  array.forEach((row) => selectedIds.value.push(row.id))
}

/** 初始化 **/
onMounted(() => {
  getList()
  console.log(getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING))
})
</script>
